//
//  YRJShowCenterViewController.m
//  YanRenjie
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 yanrenjie. All rights reserved.
//

#import "YRJShowCenterViewController.h"
#import "YRJTripHeader.h"

@interface YRJShowCenterViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *product_listArray;
@property(nonatomic, retain)MBProgressHUD *hud;
@end

NSInteger startIndex = 0;
@implementation YRJShowCenterViewController
- (void)dealloc {
    [_tableView release];
    [_product_listArray release];
    [_hud release];
    
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"秀一秀";
    self.navigationController.navigationBar.translucent = NO;
    // 修改导航栏的背景颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0 green:190 / 255.0 blue:200 / 255.0 alpha:1];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    // 注册
    [self.tableView registerClass:[YRJShowCenterTableViewCell class] forCellReuseIdentifier:@"showCenterReuse"];
    self.tableView.rowHeight = (260.0 / 667) * HEIGHT;
    
    // 添加刷新, 加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    self.tableView.footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        startIndex += 20;
        [self loadingAction];
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    self.hud.labelText = @"Loading...";
    [self createData];
    
}


- (void)createData {
    NSString *strURL = [NSString stringWithFormat:@"%@%ld%@", @"http://api.breadtrip.com/hunter/products/more/?start=", (long)startIndex, @"&city_name=%E5%85%A8%E9%83%A8%E5%9F%8E%E5%B8%82&lat=38.88272756400309&lng=121.53898315009309"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        self.product_listArray = [YRJShowCenterModel baseModelByArr:dic[@"product_list"]];
        [self.tableView reloadData];
        self.hud.hidden = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


#pragma mark - tableView 的加载刷新方法
- (void)loadingAction {
    NSString *strURL = [NSString stringWithFormat:@"%@%ld%@", @"http://api.breadtrip.com/hunter/products/more/?start=", (long)startIndex, @"&city_name=%E5%85%A8%E9%83%A8%E5%9F%8E%E5%B8%82&lat=38.88272756400309&lng=121.53898315009309"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *tempArray = [YRJShowCenterModel baseModelByArr:dic[@"product_list"]];
        for (YRJShowCenterModel *tempModel in tempArray) {
            [self.product_listArray addObject:tempModel];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (void)refreshAction {
    [self.product_listArray removeAllObjects];
    [self loadingAction];
}



#pragma mark - tableView 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.product_listArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YRJShowCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"showCenterReuse"];
    if (self.product_listArray.count != 0) {
        YRJShowCenterModel *tempModel = self.product_listArray[indexPath.row];
        [cell.titlePageImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.title_page] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        [cell.avatar_lImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.user[@"avatar_l"]] placeholderImage:[UIImage imageNamed:@"placeHold.png"]];
        cell.titleLabel.text = tempModel.title;
        cell.date_strLabel.text = [NSString stringWithFormat:@"%@ * %@ * %@喜欢", tempModel.date_str, tempModel.address, tempModel.like_count];
        NSArray *tempArr = [NSArray array];
        tempArr = tempModel.tab_list;
        NSMutableString *tempStr = [[NSMutableString alloc] init];
        for (NSString *str in tempArr) {
            [tempStr stringByAppendingString:[NSString stringWithFormat:@" * %@", str]];
        }
        cell.tab_listLabel.text = tempStr;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YRJShowCenterWebViewController *showCenterWebVC = [[YRJShowCenterWebViewController alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:showCenterWebVC animated:YES];
    YRJShowCenterModel *tempModel = self.product_listArray[indexPath.row];
    showCenterWebVC.product_id = tempModel.product_id;
    [showCenterWebVC release];
}


#pragma mark - push到下一页的时候隐藏Tabbar 
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    animated = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
